<template>
  <div>
    <h1>login</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.user_name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <router-link to="/register">
      <el-button>注册</el-button>
    </router-link>
  </div>
</template>

<script>
// 局部注册
import { Button, Form, FormItem, Input } from "element-ui";
import ajax from "../ajax/ajax";
export default {
  name: "",
  props: [""],
  data() {
    return {
      form: {
        user_name: "",
        password: "",
      },
    };
  },

  components: {
    "el-button": Button,
    "el-form": Form,
    "el-form-item": FormItem,
    "el-input": Input,
  },

  computed: {},

  beforeMount() {},

  mounted() {},

  methods: {
    async onSubmit() {
      console.log(this.form);
      const result = await ajax("post", "login", this.form, "form");
      console.log(result);
      localStorage.setItem("token", result.token);
      // 当登录成功后，进入主页  使用编程式路由导航
      this.$router.push("/content");
    },
  },

  watch: {},
};
</script>
<style lang='' scoped>
</style>